/**
 * Utility functions for Alfresco CMIS Services.	
 */

/**
 * CMISUtils root namespace.
 */
if (typeof CMISUtils == "undefined" || !CMISUtils)
{
   var CMISUtils = {};
}

/**
 * CMIS Namespaces.
 */

CMISUtils.ns = {

	cmisNS : new Namespace('http://docs.oasis-open.org/ns/cmis/core/200901'),
	atomNS : new Namespace('http://www.w3.org/2005/Atom'),
	appNS  : new Namespace('http://www.w3.org/2007/app'),
	alfNS  : new Namespace('http://www.alfresco.org')

};

/**
 * CMISUtils xml related functions.
 */

CMISUtils.xml = function() {

	function a() {
	
	}
	
	function b() {
	
	}
	
	return {
	
		getXml: function(input) {

			// Process xml return

			var str = new String(input);	

			//Javascript E4X module has problems with XML header

			if ( str.substr(0,5).indexOf("?xml") != -1 ) {
				positionRootElement = str.indexOf("<", 10);//get first real tag
				str = str.substr( positionRootElement, str.length - 1 ); 
			}

			var xmlDoc = new XML(str);
        	
        	return xmlDoc;
            
        },
        
		d: function() {
        	        	
            
        }
	}
	
} ();

/**
 * CMISUtils ticket related functions.
 */

CMISUtils.ticket = function() {

		function a() {

		}

		function b() {

		}

	
	return {
	
		getTicket: function(connector) {

			var userName = connector.getDescriptor().getUsername();
			var password = connector.getDescriptor().getPassword();

			var ticketResult = connector.call("/api/login?u="+userName+"&pw="+password);

			var ticketDoc = CMISUtils.xml.getXml(ticketResult);

			return ticketDoc.toString();
            
        },
        
		d: function() {
        	        	
            
        }
	}
	
} ();

/**
 * CMISUtils endpoint configuration related functions.
 */

CMISUtils.endpoint = function() {
	
	return {
	
		getEndpoint: function() {			

			return (args.endpoint == null) ? "alfresco":args.endpoint;
            
        }
	}
	
} ();

